<?php
declare(strict_types=1);

namespace app\apisell\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Db;

class syncTiaoJia extends Command
{
    protected function configure()
    {
        // 命令的名称及用途描述
        $this->setName('tiaojia')->setDescription('分销商品调价的定时任务');
    }
    /**
     *分销商品调价的定时任务
     *
     * @param Input $input 输入对象，用于获取命令行输入的参数
     * @param Output $output 输出对象，用于向命令行输出信息
     */
    protected function execute(Input $input, Output $output)
    {

        try {
            //获取状态为0的未执行的所有数据
            $time = date("Y-m-d H:i:s");
            $task_info = db::name('sell_goods_price_change_task')->where("  status =  0  and  effective_time  <  '".$time ."'" )->select();


            $last_id = 0;
            foreach($task_info as $k =>$v){
                //下架所有的对应的推广链接
                $popu_update['link_status'] = 0;
                $last_id = db::name('sell_popu_link')->where('goods_id = ' . $v['goods_id'])->save($popu_update);

                //获取商品变动之前的价格
                $goods_info = db::name('sell_goods')->where(" id =  " . $v['goods_id'])->find();

                //修改商品的数据
                $goods_update['price'] = $v['price'];
                $goods_update['market_price'] = $v['market_price'];
                $goods_update['commission'] = $v['commission'];
                $last_id = db::name('sell_goods')->where('id = ' . $v['goods_id'])->save($goods_update);


                //添加商品变动记录
                $goods_log_update['goods_id'] = $v['goods_id'];
                $goods_log_update['type'] = '价格变动';
                $goods_log_update['desc'] = '价格变动';
                $goods_log_update['change_json'] = '';
                $goods_log_update['change_time'] = date("Y-m-d H:i:s");
                $goods_log_update['operate_id'] = 0;
                $json_arr[0]['field'] = 'price';
                $json_arr[0]['former'] = $goods_info['price']/100;
                $json_arr[0]['after'] = $v['price']/100;
                $json_arr[0]['money'] = $goods_info['price']/100 - $v['price']/100;

                $json_arr[1]['field'] = 'commission';
                $json_arr[1]['former'] = $goods_info['commission']/100;
                $json_arr[1]['after'] = $v['commission']/100;
                $json_arr[1]['money'] = $goods_info['commission']/100 - $v['commission']/100;

                $goods_log_update['change_json'] = json_encode($json_arr);

                $last_id = db::name('sell_goods_price_log')->save($goods_log_update);

                // 执行完了以后把sell_goods_price_change_task 里面的状态改成 1  表示已经执行过
                $task_update_data['status'] = 1;
                $last_id = db::name('sell_goods_price_change_task')->where('id = ' . $v['id'])->save($task_update_data);
            }

            // 成功以后 删除输入输出信息
            if ($last_id) {
                $output->writeln("成功执行调价的定时任务");
            } else {
                $output->writeln("没有成功执行调价的定时任务");
            }
        } catch (\Exception $e) {
            $output->writeln('执行调价的定时任务抛异常,状态-失败Exception: ' . $e->getMessage());
        }
    }
    
    
}